//
//  TextTableViewCell.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/17.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "TextTableViewCell.h"
#import "TextModel.h"
#import "UIImageView+WebCache.h"

#define TextFont [UIFont systemFontOfSize:15]

@interface TextTableViewCell ()
/**昵称label*/
@property (nonatomic, strong) UILabel *nameLabel;

/**发布时间label*/
@property (nonatomic, strong) UILabel *timeLabel;

/**头像imageView*/
@property (nonatomic, strong) UIImageView *iconImage;

/**正文label*/
@property (nonatomic, strong) UILabel *textLabell;

/**顶一下label*/
@property (nonatomic, strong) UILabel *loveLabel;

/**踩一下label*/
@property (nonatomic, strong) UILabel *hateLabel;

/**顶一下button*/
@property (nonatomic, strong) UIButton *buttonLove;

/**踩一下button*/
@property (nonatomic, strong) UIButton *buttonHate;

/**收藏button*/
@property (nonatomic, strong) UIButton *collectButton;

@end


@implementation TextTableViewCell

//重写cell的init方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
       
        _nameLabel = [[UILabel alloc] init];
        
        _timeLabel = [[UILabel alloc] init];
        
        _iconImage = [[UIImageView alloc] init];
        
        _textLabell = [[UILabel alloc] init];
        _textLabell.font = TextFont;
        
        _loveLabel = [[UILabel alloc] init];
        
        _hateLabel = [[UILabel alloc] init];
        
        _buttonLove = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        _buttonHate = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        _collectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        [self.contentView addSubview:_nameLabel];
        
        [self.contentView addSubview:_timeLabel];
        
        [self.contentView addSubview:_iconImage];
        
        [self.contentView addSubview:_textLabell];
        
        [self.contentView addSubview:_loveLabel];
        
        [self.contentView addSubview:_hateLabel];
        
        [self.contentView addSubview:_buttonLove];
        
        [self.contentView addSubview:_buttonHate];
        
        [self.contentView addSubview:_collectButton];
    }
    return self;
}

//子控件布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat margin = 10;
    
    self.iconImage.frame = CGRectMake(margin, margin, 50, 50);
    //self.iconImage.backgroundColor = [UIColor redColor];
    
    self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame) + margin, margin, 150, 30);
    //self.nameLabel.backgroundColor = [UIColor blueColor];
    
    self.timeLabel.frame = CGRectMake(CGRectGetMinX(self.nameLabel.frame), CGRectGetMaxY(self.nameLabel.frame) + 5, 120, 20);
    //self.timeLabel.backgroundColor = [UIColor yellowColor];
    
    self.collectButton.frame = CGRectMake(CGRectGetMaxX(self.nameLabel.frame) +100, CGRectGetMinY(self.nameLabel.frame) + margin, 32, 32);
    [self.collectButton setImage:[UIImage imageNamed:@"iconfont-shoucang-1"] forState:(UIControlStateNormal)];
    [self.collectButton setImage:[UIImage imageNamed:@"iconfont-shoucang(1)"] forState:(UIControlStateSelected)];
    [self.collectButton addTarget:self action:@selector(buttonClick) forControlEvents:(UIControlEventTouchUpInside)];
    
//    CGSize textSize = [self sizeWithText:self.textModel.text font:TextFont maxSize:(CGSizeMake(355, MAXFLOAT))];
    
    self.textLabell.frame = CGRectMake(CGRectGetMinX(self.iconImage.frame), CGRectGetMaxY(self.iconImage.frame) + margin +20, self.bounds.size.width - 20, 20);
    _textLabell.numberOfLines = 0;
    [_textLabell sizeToFit];
    
    _buttonLove.frame = CGRectMake(180, CGRectGetMaxY(self.textLabell.frame) + margin, 50, 30);
    [_buttonLove setTitle:@"顶" forState:(UIControlStateNormal)];
    [_buttonLove setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [_buttonLove setImage:[UIImage imageNamed:@"iconfont-zan(1)"] forState:(UIControlStateNormal)];
    //_buttonLove.backgroundColor = [UIColor yellowColor];
    
    self.loveLabel.frame = CGRectMake(CGRectGetMaxX(self.buttonLove.frame), CGRectGetMinY(self.buttonLove.frame), 50, 30);
    //self.loveLabel.backgroundColor = [UIColor redColor];
    
    _buttonHate.frame = CGRectMake(CGRectGetMaxX(self.loveLabel.frame), CGRectGetMinY(self.loveLabel.frame), 50, 30);
    [_buttonHate setTitle:@"踩" forState:(UIControlStateNormal)];
    [_buttonHate setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [_buttonHate setImage:[UIImage imageNamed:@"iconfont-cai(1)"] forState:(UIControlStateNormal)];
    
    
    self.hateLabel.frame = CGRectMake(CGRectGetMaxX(self.buttonHate.frame), CGRectGetMinY(self.buttonHate.frame), 50, 30);
    
}
- (void)buttonClick
{
    NSLog(@"-------buttonClick---------");
    if (self.collectButton.selected == NO) {
        
        self.collectButton.selected = YES;
    }else{
        
        self.collectButton.selected = NO;
    }
}
//重写textModel的set方法
- (void)setTextModel:(TextModel *)textModel
{
    _textModel = textModel;
    
    [self.iconImage setImageWithURL:[NSURL URLWithString:textModel.profile_image]];
    
    self.nameLabel.text = textModel.name;
    
    NSRange range = NSMakeRange(5, 11);
    self.timeLabel.text = [NSString stringWithFormat:@"%@",[textModel.create_time substringWithRange:range]];
    
    self.textLabell.text = textModel.text;
    
    self.loveLabel.text = textModel.love;
    
    self.hateLabel.text = textModel.hate;
    
    
}

//// 封装一个根据字符串\字体\最大大小,来计算大小的方法
//- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
//{
//    NSDictionary *attrs = @{NSFontAttributeName : font};
//    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
//}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
